package com.smartens.boit.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.smartens.boit.R;
import com.smartens.boit.adapter.ListAdapter;
import com.smartens.boit.bean.FaultBean;
import com.smartens.boit.bean.ListBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

public class FaultHistoryActivity extends AppCompatActivity {
    private LinearLayout back;
    private RecyclerView mlist;
    private List<ListBean> mdata;
    private ListAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private HttpHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fault_history);
        back = findViewById(R.id.back);
//        找到控件
        mlist = findViewById(R.id.recycler_view);
        mRefreshLayout = findViewById(R.id.refresh_layout);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FaultHistoryActivity.this.finish();
            }
        });
        helper = HttpHelper.getInstance();
        GetDeviceFaultLists();
//        下拉刷新
        DownPullUpdate();


    }

    public void GetDeviceFaultLists() {
        helper.GetDeviceFaultList(new HttpHelper.HttpCallBack<ListBean>() {
            @Override
            public void onSuccess(List<ListBean> result) {
                mdata = new ArrayList();
                mdata = result;
                //                准备数据
                back.post(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                    }
                });



            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    private void DownPullUpdate() {
        mRefreshLayout.setEnabled(true);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                执行刷新的逻辑处理
                GetDeviceFaultLists();
//                更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        更新列表
                        mAdapter.notifyDataSetChanged();
//                        刷新停止
                        mRefreshLayout.setRefreshing(false);

                    }
                }, 1000);
            }
        });
    }

    private void initListener() {
        mAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(FaultHistoryActivity.this, "设备名称为" + mdata.get(position).getDeviceName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
//        List<DataBean> --------- Adapter -------- SetAdapter -------- 显示数据
//        RecyclerView需要设置布局样式
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mlist.setLayoutManager(linearLayoutManager);
//        创建适配器
        mAdapter = new ListAdapter(mdata);
//        设置Adapter到RecyclerView里去
        mlist.setAdapter(mAdapter);
        //        创建监听事件
        initListener();
    }
}